package eafit.sicom.calculadorapromedios;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

public class WheelGradeActivity extends Activity {

	public static final String GRADE_DESIRED = "GRADE_DESIRED";

	String i;
	String d;
	Double desiredGradeSelected;

	private boolean scrolling = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.grade_dialog_layout);

		final WheelView integerGrade = (WheelView) findViewById(R.id.grade_integer);
		integerGrade.setVisibleItems(3);
		final String integers[] = new String[] { "0.", "1.", "2.", "3.", "4.",
				"5." };
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(this,
				integers);
		adapter.setTextSize(18);
		integerGrade.setViewAdapter(adapter);

		final String[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
				"9" };
		String[] zero = { "0" };
		final String decimalsArray[][] = new String[][] { digits, digits,
				digits, digits, digits, zero

		};

		final WheelView decimalGrade = (WheelView) findViewById(R.id.grade_decimals);
		decimalGrade.setVisibleItems(3);

		integerGrade.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {

					updateDecimals(decimalGrade, decimalsArray, newValue);
					i = integers[integerGrade.getCurrentItem()];
					d = digits[decimalGrade.getCurrentItem()];

				}
			}

		});

		integerGrade.addScrollingListener(new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
				// gI = integerGrade.getCurrentItem();
				i = integers[integerGrade.getCurrentItem()];
				updateDecimals(decimalGrade, decimalsArray,
						integerGrade.getCurrentItem());

			}
		});

		integerGrade.setCurrentItem(3);

		decimalGrade.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// gD = decimalGrade.getCurrentItem();
				d = digits[decimalGrade.getCurrentItem()];

			}
		});

		decimalGrade.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// Nothing here!

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				d = digits[decimalGrade.getCurrentItem()];

			}
		});

	}

	public void updateDecimals(WheelView decimal, String decimals[][], int index) {
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(this,
				decimals[index]);
		adapter.setTextSize(18);
		decimal.setViewAdapter(adapter);
		decimal.setCurrentItem(decimals[index].length / 2);
	}

	public void returnSelectedWheels(View view) {

		Intent resultIntent = new Intent(this, MainActivity.class);

		resultIntent.putExtra(GRADE_DESIRED, i + d);

		setResult(Activity.RESULT_OK, resultIntent);
		finish();

		finish();

	}
}
